package top.superxuqc.loginbili.controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import top.superxuqc.loginbili.service.DownVideoService;
import top.superxuqc.loginbili.service.LoginService;
import top.superxuqc.loginbili.util.MyHttpClient;

import javax.jws.WebParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InitPage {

    @Autowired
    private LoginService login;

    @Autowired
    private DownVideoService ds;
    @RequestMapping("/x/v/c/check/cookie")
    public ModelAndView seeCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies !=null){
            for (Cookie c:cookies
            ) {
                if(c.getName().equals("DedeUserID")){
                    ModelAndView mv = new ModelAndView("successLogin");
                    mv.addObject("tops",ds.getTop());
                    mv.addObject("likes",ds.getLike(request).toString());
                    return mv;
                }

            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("loginApp");
        Map<String, String> stringStringMap = login.solveNoAI();
        mv.addObject("gt",stringStringMap.get("gt"));
        mv.addObject("challenge",stringStringMap.get("challenge"));
        mv.addObject("key",stringStringMap.get("key"));
        return mv;
    }
    @RequestMapping("/1")
    public ModelAndView test(HttpServletRequest request){
        //JSONArray bv1dh411m7be = ds.getFlvVideo("BV1dh411m7be", request);
        return new ModelAndView("play");
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @RequestMapping("/control")
    public ModelAndView c(HttpServletRequest request){
        //JSONArray bv1dh411m7be = ds.getFlvVideo("BV1dh411m7be", request);
        String oldNew = redisTemplate.opsForValue().get("info_first");
        String oldHas = redisTemplate.opsForValue().get("has_info");
        String oldGong = redisTemplate.opsForValue().get("info_info");
        String oldVersion = redisTemplate.opsForValue().get("version");
        String oldInfo = redisTemplate.opsForValue().get("version_info");
        ModelAndView mv = new ModelAndView("control");
        mv.addObject("oldNew",oldNew);
        mv.addObject("oldHas",oldHas);
        mv.addObject("oldGong",oldGong);
        mv.addObject("oldVersion",oldVersion);
        mv.addObject("oldInfo",oldInfo);

        return mv;
    }
    @RequestMapping("/2")
    @ResponseBody
    public String test2(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String coo = "";
        if(cookies !=null){
            for (Cookie c:cookies
            ) {

                coo += c.getName()+"="+c.getValue()+";";

            }
        }
        return coo;
    }

    @RequestMapping("/phone")
    public ModelAndView initpLogin(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies !=null){
            for (Cookie c:cookies
            ) {
                if(c.getName().equals("DedeUserID")){
                    ModelAndView mv = new ModelAndView("Video");
                    mv.addObject("tops",ds.getTop());
                    mv.addObject("likes",ds.getLike(request).toString());
                    return mv;
                }

            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("PhoneLogin");
        Map<String, String> stringStringMap = login.solveNoAI();
        mv.addObject("gt",stringStringMap.get("gt"));
        mv.addObject("challenge",stringStringMap.get("challenge"));
        mv.addObject("key",stringStringMap.get("key"));
        return mv;
    }

    @RequestMapping("/appphone")
    public ModelAndView initapppLogin(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies !=null){
            for (Cookie c:cookies
            ) {
                if(c.getName().equals("DedeUserID")){
                    ModelAndView mv = new ModelAndView("Video");
                    mv.addObject("tops",ds.getTop());
                    mv.addObject("likes",ds.getLike(request).toString());
                    return mv;
                }

            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("appPhoneLogin");
        Map<String, String> stringStringMap = login.solveNoAI();
        mv.addObject("gt",stringStringMap.get("gt"));
        mv.addObject("challenge",stringStringMap.get("challenge"));
        mv.addObject("key",stringStringMap.get("key"));
        return mv;
    }

    @RequestMapping("/")
    public ModelAndView initLogin(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies !=null){
            for (Cookie c:cookies
                 ) {
                if(c.getName().equals("DedeUserID")){
                    ModelAndView mv = new ModelAndView("Video");
                    mv.addObject("tops",ds.getTop());
                    mv.addObject("likes",ds.getLike(request).toString());
                    return mv;
                }

            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        Map<String, String> stringStringMap = login.solveNoAI();
        mv.addObject("gt",stringStringMap.get("gt"));
        mv.addObject("challenge",stringStringMap.get("challenge"));
        mv.addObject("key",stringStringMap.get("key"));
        return mv;
    }
}
